//
//  CYActionManager.m
//  cyjf
//
//  Created by tauCross on 2017/5/24.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYActionManager.h"

#import "CYWebViewController.h"
#import "CYMainController.h"
#import "CYFinanceDetailViewController.h"
#import "CYBindCardViewController.h"
#import "CYNavigationController.h"
#import "CYCouponsViewController.h"

#import "CYGlobal.h"


@implementation CYActionManager

+ (instancetype)standardManager
{
    static CYActionManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[CYActionManager alloc] init];
    });
    return manager;
}

- (void)executeActionWithModel:(CYDataAPIActionModel *)model fromViewController:(UIViewController *)viewContoller
{
    if(model.type == nil)
    {
        return;
    }
    switch(model.type.integerValue)
    {
        case 1:
        {
            NSString *url = model.ext;
            if(url.tc_url)
            {
                CYWebViewController *webVieController = [[CYWebViewController alloc] init];
                webVieController.url = model.ext;
                if(viewContoller.navigationController)
                {
                    [viewContoller.navigationController pushViewController:webVieController animated:YES];
                }
                else
                {
                    [viewContoller presentViewController:[[CYNavigationController alloc] initWithRootViewController:webVieController] animated:YES completion:nil];
                }
            }
            break;
        }
        case 2:
        {
            [[CYMainController standardController] goFinance];
            break;
        }
        case 3:
        {
            NSNumber *fid = @([model.ext longLongValue]);
            if(fid.longLongValue > 0)
            {
                CYFinanceDetailViewController *financeDetailViewController = [[CYFinanceDetailViewController alloc] init];
                financeDetailViewController.fid = fid;
                if(viewContoller.navigationController)
                {
                    [viewContoller.navigationController pushViewController:financeDetailViewController animated:YES];
                }
                else
                {
                    [[CYMainController standardController] goFinance];
                    [[[CYMainController standardController] financeNavigationController] pushViewController:financeDetailViewController animated:NO];
                }
            }
            break;
        }
        case 4:
        {
            if(CYStandardAuthManager.currentAuth)
            {
                [[CYMainController standardController] goMine];
            }
            else
            {
                [[CYMainController standardController] showLogIn];
            }
            break;
        }
        case 5:
        {
            if(CYStandardAuthManager.currentAuth)
            {
                CYBindCardViewController *bindCardViewController = [[CYBindCardViewController alloc] init];
                bindCardViewController.type = CYBindCardTypeNormal;
                if(viewContoller.navigationController)
                {
                    [viewContoller.navigationController pushViewController:bindCardViewController animated:YES];
                }
                else
                {
                    [[CYMainController standardController] goMine];
                    [[[CYMainController standardController] mineNavigationController] pushViewController:bindCardViewController animated:NO];
                }
            }
            else
            {
                [[CYMainController standardController] showLogIn];
            }
            break;
        }
        case 6:
        {
            [[CYMainController standardController] goHome];
            break;
        }
        case 7:
        {
            void (^block)(void) = ^(void){
                [[CYMainController standardController] goCoupon];
            };
            if(CYStandardAuthManager.currentAuth)
            {
                block();
            }
            else
            {
                CYSharedGlobal.loginAction = block;
                [[CYMainController standardController] showLogIn];
            }
            break;
        }
        case 8:
        {
            [[CYMainController standardController] goDiscover];
            break;
        }
        default:
            break;
    }
}

@end
